//
//  FlutterOpenglViewFactory.swift
//  flutter_test_opengl_plugin
//
//  Created by yuanxc on 2024/5/23.
//

import Foundation
import FlutterMacOS

class FlutterOpenglViewFactory: NSObject, FlutterPlatformViewFactory {
    private var message : FlutterBinaryMessenger;

    init(message : FlutterBinaryMessenger) {
        self.message = message;
    }

    func createArgsCodec() -> (FlutterMessageCodec & NSObjectProtocol)? {
        FlutterStandardMessageCodec.sharedInstance()
    }

    func create(withViewIdentifier viewId: Int64, arguments args: Any?) -> NSView {
        let pixelFormatAttributes: [NSOpenGLPixelFormatAttribute] = [
            UInt32(NSOpenGLPFAOpenGLProfile),
            UInt32(NSOpenGLProfileVersion3_2Core),
            UInt32(NSOpenGLPFAColorSize), 24,
            UInt32(NSOpenGLPFAAlphaSize), 8,
            UInt32(NSOpenGLPFADepthSize), 24,
            UInt32(NSOpenGLPFAStencilSize), 8,
            UInt32(NSOpenGLPFADoubleBuffer),
            0
        ]

        guard let pixelFormat = NSOpenGLPixelFormat(attributes: pixelFormatAttributes) else {
            fatalError("Failed to create pixel format")
        }
//        let glView = GLCoreProfileView(frame: .zero, pixelFormat: pixelFormat)
        let glView = OpenglCCView(frame: .zero, pixelFormat: pixelFormat)
        return glView ?? NSView()
    }
}
